import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpService {

    DatagramSocket socket;


    public UdpService(int port) throws SocketException {

        socket=new DatagramSocket(port);

    }


    public void start() throws IOException {


        System.out.println("服务器启动");

        while(true){

            DatagramPacket packet=new DatagramPacket(new byte[4096],4096);
            socket.receive(packet);

            String request=new String(packet.getData(),0, packet.getLength());

            String respon=process(request);

            DatagramPacket packetrespon=new DatagramPacket(respon.getBytes(),0,respon.getBytes().length,packet.getSocketAddress());

            socket.send(packetrespon);
            System.out.println("port:"+packetrespon.getPort()+"ip"+packetrespon.getAddress());




        }

    }

    private String process(String request) {

        return request;
    }


    public static void main(String[] args) throws IOException {

        UdpService service=new UdpService(9090);

        service.start();



    }

}
